package com.yanqu.road.server.cmd.activity.lanruosi;


import com.yanqu.road.entity.activity.lanruosi.LanRuoSiConfig;
import com.yanqu.road.entity.activity.lanruosi.LanRuoSiUserData;
import com.yanqu.road.pb.activity.LanRuoSiProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.lanruosi.LanRuoSiModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.lanruosi.LanRuoSiMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_LANRUOSI_USE_ENERGY_ITEM, desc = "使用体力药")
public class LanRuoSiUseEnergyItemCmd extends AbstractCommand {

    private int count = 0;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        LanRuoSiProto.LanRuoSiUseEnergyItemReqMsg reqMsg = LanRuoSiProto.LanRuoSiUseEnergyItemReqMsg.parseFrom(packet.getBytes());
        count = reqMsg.getCount();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        LanRuoSiConfig drinkConfig = LanRuoSiMgr.getConfig();
        if (drinkConfig == null || !ActivityMgr.activityInTime(drinkConfig.getActivityInfo())) {
            ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_OPEN;
            return;
        }

        LanRuoSiModule module = player.getModule(LanRuoSiModule.class);
        LanRuoSiUserData userData = module.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_UNLOCK;
            return;
        }

        ret = module.useEnergyItem(count);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            LanRuoSiProto.LanRuoSiUseEnergyItemRespMsg.Builder builder = LanRuoSiProto.LanRuoSiUseEnergyItemRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_LANRUOSI_USE_ENERGY_ITEM, builder);
        }
    }
}
